from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, SelectField, BooleanField, PasswordField, TextAreaField, \
    SelectMultipleField, IntegerField
from flask_wtf.file import FileField, FileAllowed, FileRequired
from wtforms.validators import DataRequired, Email, Length,  EqualTo, URL, Optional
from app.models import Category, Tag


class LoginForm(FlaskForm):
    email = StringField('注册邮箱', validators=[DataRequired()], render_kw={'placeholder': '邮箱'})
    password = PasswordField('密码', validators=[DataRequired(), Length(6, 128, message='密码长度好像不太对')], render_kw={'placeholder': '密码'})
    remember = BooleanField('记住我')
    submit = SubmitField('登录')


class RegisterForm(FlaskForm):
    email = StringField('注册邮箱', validators=[DataRequired()], render_kw={'placeholder': '邮箱'})
    password = PasswordField('密码', validators=[DataRequired(), Length(6, 128, message='长度至少6位'), EqualTo('password2')], render_kw={'placeholder': '密码，最少6位'})
    password2 = PasswordField('确认密码', validators=[DataRequired()], render_kw={'placeholder': '确认密码'})
    submit = SubmitField('注册')


class ChangePassword(FlaskForm):
    old_password = StringField('旧密码', validators=[DataRequired()])
    new_password = StringField('新密码', validators=[DataRequired(), Length(0, 128), EqualTo('confirm_password')])
    confirm_password = StringField('确认密码', validators=[DataRequired()])
    submit = SubmitField('修改密码')


class CrawlerEditForm(FlaskForm):
    name = StringField('名称', validators=[DataRequired()], render_kw={'placeholder': '爬虫名称'})
    category = SelectField('选择分类', validators=[DataRequired()], coerce=int)
    rate = StringField('更新频率', render_kw={'placeholder': '单位为秒，默认86400'})
    source = StringField('数据源')
    describe = TextAreaField('描述', validators=[DataRequired()], render_kw={'placeholder': '爬虫介绍，出现在订阅列表里'})
    slug = StringField('家族标签', render_kw={'placeholder': '如zhihu，weibo，相同的slug为同一个家族'})
    tag = StringField('标签(用空格隔开)', validators=[Optional(), Length(0, 64)],
                      render_kw={'placeholder': 'tag1 tag2 tag3'})
    type = SelectField('类型', coerce=int, choices=[(0, 'RSS'), (1, '爬虫1号'), (2, '特殊处理')])
    image = FileField('图片', validators=[FileRequired(), FileAllowed(['jpg', 'jpeg', 'gif', 'png'])],
                      render_kw={'placeholder': '1200*630左右'})
    submit = SubmitField('提交')

    def __init__(self, *args, **kwargs):
        super(CrawlerEditForm, self).__init__(*args, **kwargs)
        self.category.choices = [(category.id, category.title) for category in Category.query.all()]


class SuggestForm(FlaskForm):
    website = StringField('网址', validators=[DataRequired(), URL()], render_kw={'placeholder': '希望订阅的网址'})
    describe = TextAreaField('描述', validators=[DataRequired()], render_kw={'placeholder': '简单说明，需要哪些内容或分类'})
    email = StringField('注册邮箱', validators=[DataRequired()], render_kw={'placeholder': '若订阅被我们添加，我们会用邮件通知您'})
    submit = SubmitField('提交')


class UserEditForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired()])
    email = StringField('邮箱', validators=[DataRequired()])
    password = PasswordField('密码', render_kw={'placeholder': '不修改则留空'})
    submit = SubmitField('确认修改')


class CategoryForm(FlaskForm):
    title = StringField('分类名', validators=[DataRequired()])
    submit = SubmitField('提交')
